package org.rcgwt.client.ui.validator.impl;

import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.IFormatter;
import org.rcgwt.client.ui.validator.ISingleton;
import org.rcgwt.client.ui.validator.IValidatorElement;

/**
 * A formatter which format an INSEE number by elements (like S YY MM DP CIT ORD
 * KE)
 * 
 * @author Elian ORIOU
 * 
 */

public class INSEEFormatter implements IFormatter, ISingleton {

	private static final INSEEFormatter INSTANCE = new INSEEFormatter();

	/* INSEE elements positions */
	private final int SEX_FIELD_POS = 0;
	private final int YEAR_FIELD_POS = 2;
	private final int MONTH_FIELD_POS = 4;
	private final int DEPT_FIELD_POS = 6;
	private final int CITY_FIELD_POS = 9;
	private final int ORDER_FIELD_POS = 12;

	protected INSEEFormatter() {

	}

	@Override
	public String format(IClientValidatorContext context, String ttf) {
		String s = "";
		for (int i = 0; i < ttf.length(); i++) {
			/* Adding space in the string at element position */
			s += ttf.charAt(i);
			switch (i) {
			case SEX_FIELD_POS:
			case YEAR_FIELD_POS:
			case MONTH_FIELD_POS:
			case DEPT_FIELD_POS:
			case CITY_FIELD_POS:
			case ORDER_FIELD_POS:
				s += " ";
				break;
			}
		}
		return s;
	}

	public static IValidatorElement getInstance() {
		return INSTANCE;
	}
}
